//
//  PPRegisterViewController.m
//  PetParadise
//
//  Created by 陈无忌 on 8/2/14.
//  Copyright (c) 2014 whalefin. All rights reserved.
//

#import "PPRegisterViewController.h"
#import "PPRegisterPetTableViewController.h"
#import "supportingHeadFiles.h"

@interface PPRegisterViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) NSTimer *countDownTimer;
@property (nonatomic) int secondsCountDown;
@property (nonatomic) BOOL canGet;


@property (weak, nonatomic) IBOutlet UIButton *maleGenderButton;
@property (weak, nonatomic) IBOutlet UIButton *femaleGenderButton;

@property (weak, nonatomic) IBOutlet UIView *nickNameView;
@property (weak, nonatomic) IBOutlet UITextField *nickNameTextFiled;
@property (weak, nonatomic) IBOutlet UIView *genderView;
//@property (weak, nonatomic) IBOutlet UITextField *genderTextFiled;
@property (weak, nonatomic) IBOutlet UIView *locationNoView;
@property (weak, nonatomic) IBOutlet UILabel *locationNoLabel;
@property (weak, nonatomic) IBOutlet UIView *teleView;
@property (weak, nonatomic) IBOutlet UITextField *teleTextFiled;
@property (weak, nonatomic) IBOutlet UIView *passwordView;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextFiled;


@property (weak, nonatomic) IBOutlet UIView *verificationCodeView;
@property (weak, nonatomic) IBOutlet UILabel *buttonLabel;
@property (weak, nonatomic) IBOutlet UITextField *verificationCodeTextField;

@property (nonatomic) int gender;

@property (nonatomic) int registerId;


@end

@implementation PPRegisterViewController



- (void)choseMale:(UIButton *)sender {
    self.gender = sender.tag;
    [self.maleGenderButton setImage:[UIImage imageNamed:@"maleBig"] forState:UIControlStateNormal];
    [self.femaleGenderButton setImage:[UIImage imageNamed:@"female"] forState:UIControlStateNormal];
}

- (void)choseFemale:(UIButton *)sender {
    self.gender = sender.tag;
    [self.maleGenderButton setImage:[UIImage imageNamed:@"male"] forState:UIControlStateNormal];
    [self.femaleGenderButton setImage:[UIImage imageNamed:@"femaleBig"] forState:UIControlStateNormal];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self staticViewStyleSet];
    
    self.secondsCountDown = 60;
    self.canGet = YES;
    self.buttonLabel.text = @"获取验证码";
}

- (void)staticViewStyleSet
{
    
    [self.maleGenderButton addTarget:self action:@selector(choseMale:) forControlEvents:UIControlEventTouchUpInside];
    [self.femaleGenderButton addTarget:self action:@selector(choseFemale:) forControlEvents:UIControlEventTouchUpInside];
    self.maleGenderButton.tag = 0;
    self.femaleGenderButton.tag = 1;
    
    self.gender = 0;
    [self.maleGenderButton setImage:[UIImage imageNamed:@"maleBig"] forState:UIControlStateNormal];
    [self.femaleGenderButton setImage:[UIImage imageNamed:@"female"] forState:UIControlStateNormal];
    //nickName View
    self.nickNameView.layer.cornerRadius = 14;
    [self.nickNameView.layer setBorderColor:[UIColor colorWithRed:194/255.0 green:193/255.0 blue:193/255.0 alpha:1.0].CGColor];
    self.nickNameView.layer.borderWidth = 1;
    //area View
    self.genderView.layer.cornerRadius = 14;
    [self.genderView.layer setBorderColor:[UIColor colorWithRed:194/255.0 green:193/255.0 blue:193/255.0 alpha:1.0].CGColor];
    self.genderView.layer.borderWidth = 1;
    //location No. view
    self.locationNoView.layer.cornerRadius = 14;
    [self.locationNoView.layer setBorderColor:[UIColor colorWithRed:194/255.0 green:193/255.0 blue:193/255.0 alpha:1.0].CGColor];
    self.locationNoView.layer.borderWidth = 1;
    //tele View
    self.teleView.layer.cornerRadius = 14;
    [self.teleView.layer setBorderColor:[UIColor colorWithRed:194/255.0 green:193/255.0 blue:193/255.0 alpha:1.0].CGColor];
    self.teleView.layer.borderWidth = 1;
    //password View
    self.passwordView.layer.cornerRadius = 14;
    [self.passwordView.layer setBorderColor:[UIColor colorWithRed:194/255.0 green:193/255.0 blue:193/255.0 alpha:1.0].CGColor];
    self.passwordView.layer.borderWidth = 1;
    //verificationCode View
    self.verificationCodeView.layer.cornerRadius = 14;
    [self.verificationCodeView.layer setBorderColor:[UIColor colorWithRed:194/255.0 green:193/255.0 blue:193/255.0 alpha:1.0].CGColor];
    self.verificationCodeView.layer.borderWidth = 1;
    //delegate
    self.nickNameTextFiled.delegate = self;
    self.teleTextFiled.delegate = self;
    self.passwordTextFiled.delegate = self;
    self.verificationCodeTextField.delegate = self;
    
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
-(void)timeFireMethod
{
    self.secondsCountDown --;
    self.buttonLabel.text = [NSString stringWithFormat:@"%d秒后重新获取",self.secondsCountDown];
//    NSLog(@"%d",self.secondsCountDown);
    if (self.secondsCountDown == 0) {
        [self.countDownTimer invalidate];
        self.canGet = YES;
        self.secondsCountDown = 60;
        self.buttonLabel.text = @"获取验证码";
    }
}

- (IBAction)back:(id)sender {
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}


- (IBAction)getVerificationCodeButton:(id)sender {
    if (self.canGet) {
        self.canGet = NO;
        self.countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
        //get VerifyCode
        NSString *url = [PPAPIList getAPIList].PPCreateVerifyCode;
        NSDictionary *params = @{@"phoneNum":self.teleTextFiled.text};
        [httpRequst POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"responseObject is:%@",responseObject);
            NSString *msg = responseObject[@"errorMsg"];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:@"返回" otherButtonTitles:nil];
            [alert show];
        }failure:^(AFHTTPRequestOperation *operation, NSError *error){
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络不给力" delegate:self cancelButtonTitle:@"返回" otherButtonTitles:nil];
            [alert show];
        }];
        
    }
}

- (IBAction)registerButton:(id)sender {
    [HUD showUIBlockingIndicatorWithText:@"正在注册"];
    NSString *url = [PPAPIList getAPIList].PPRegister;
    NSDictionary *params = @{@"phoneNum":self.teleTextFiled.text,@"code":self.verificationCodeTextField.text,@"userName":self.nickNameTextFiled.text,@"loginPassword":self.passwordTextFiled.text,@"userGender":@(self.gender)};
    [httpRequst POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        [HUD hideUIBlockingIndicator];
        NSLog(@"responseObject is:%@",responseObject);
        NSString *msg = responseObject[@"errorMsg"];
        NSNumber *restule = responseObject[@"processResult"];
        if (restule.boolValue) {
            //注册成功 获取注册完的ID
            NSDictionary *resultMap = responseObject[@"resultMap"];
            self.registerId = [resultMap[@"userId"] intValue];
            //页面跳转
            PPRegisterPetTableViewController *targetVC =[self.storyboard instantiateViewControllerWithIdentifier:@"PPRegisterPetTableViewController"];
            targetVC.userId = self.registerId;
            [self.navigationController pushViewController:targetVC animated:YES];
        }else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:@"返回" otherButtonTitles:nil];
            [alert show];
        }
        [HUD hideUIBlockingIndicator];
    }failure:^(AFHTTPRequestOperation *operation, NSError *error){
        [HUD hideUIBlockingIndicator];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络不给力" delegate:self cancelButtonTitle:@"返回" otherButtonTitles:nil];
        [alert show];
    }];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if (![self.nickNameTextFiled isExclusiveTouch]) {
        [self.nickNameTextFiled resignFirstResponder];
    }
    if (![self.teleTextFiled isExclusiveTouch]) {
        [self.teleTextFiled resignFirstResponder];
    }
    if (![self.passwordTextFiled isExclusiveTouch]) {
        [self.passwordTextFiled resignFirstResponder];
    }
    if (![self.verificationCodeTextField isExclusiveTouch]) {
        [self.verificationCodeTextField resignFirstResponder];
    }
    
    
}



@end
